package com.ifish.leetcode.problem.algorithm;

import com.ifish.leetcode.problem.BaseLeetCodeProblem;

/**
 * Created by iFish on 2017/7/11.
 */

//Given an integer, convert it to a roman numeral.
//
//        Input is guaranteed to be within the range from 1 to 3999.
//
//    I 1, V 5, X 10, L 50, C 100, D 500, M 1000, 一个单位最多重复三次
//    3999  MMM CM XC IX

public class _0012 extends BaseLeetCodeProblem {
    @Override
    public String title() {
        return "Integer to Roman";
    }

    @Override
    public int difficulty() {
        return 1;
    }

    @Override
    public void run() {
        super.run();

        int num = 3548;
        printResult(intToRoman(num));
    }

    public String intToRoman(int num) {

        // we can just simply list all possible strings here to use
//        String M[] = {"", "M", "MM", "MMM"};
//        String C[] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
//        String X[] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
//        String I[] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};

        StringBuffer sb = new StringBuffer();
        sb.append(unitOf(num / 1000, "M", "", ""));
        sb.append(unitOf(num / 100 % 10, "C", "D", "M"));
        sb.append(unitOf(num / 10 % 10, "X", "L", "C"));
        sb.append(unitOf(num % 10, "I", "V", "X"));

        return sb.toString();
    }

    private String unitOf(int num, String l, String m, String h) {
        switch (num) {
            case 1: return l;
            case 2: return l + l;
            case 3: return l + l + l;
            case 4: return l + m;
            case 5: return m;
            case 6: return m + l;
            case 7: return m + l + l;
            case 8: return m + l + l + l;
            case 9: return l + h;
            default: return "";
        }


    }
}
